﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AutoSendMailFromExecl
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        Boolean isSendMail = false;

        void showMsg(string msg)
        {
            this.tb_msg.Invoke(new Action(() =>
            {
                this.tb_msg.Text = msg + "\r\n" + this.tb_msg.Text;
            }));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tb_mail_sendfrom_pwd.Text))
            {
                showMsg("发送邮件密码不能为空");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.tb_mail_fromurl.Text))
            {
                showMsg("发送邮件地址不能为空");
                return;
            }

            lock (this)
            {
                if (isSendMail)
                {
                    return;
                }
                isSendMail = true;
            }

            if (this.openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string execlFilePath = this.openFileDialog.FileName;
                if (".xls".Equals(System.IO.Path.GetExtension(execlFilePath), StringComparison.OrdinalIgnoreCase))
                {
                    ExeclData execlData = new ExeclData();
                    execlData.Read(execlFilePath, new Action<string>((string ret) =>
                    {
                        showMsg(ret);
                    }));

                    new Thread(() =>
                    {
                        foreach (var item in execlData.Rows)
                        {
                            ExeclData.ExeclDataRow row = item.Value;
                            if (row.Cells.ContainsKey("mail"))
                            {
                                ExeclData.ExeclDataRow.ExeclDataCell mailCell = row.Cells["mail"];

                                StringBuilder sBuilder = new StringBuilder();
                                sBuilder.AppendLine("<table border='1' bordercolor='#000000' cellpadding='2' cellspacing='0' style='font-size: 10pt; border-collapse:collapse; border:none' >");
                                sBuilder.AppendLine("<tbody>");
                                sBuilder.AppendLine("<tr>");
                                foreach (var cell in row.Cells)
                                {
                                    if (cell.Value.Equals(mailCell)) { continue; }
                                    sBuilder.AppendLine("<td width='120px' style='border: solid 1 #000000' nowrap=''><font size='2' face='Verdana'><div>" + cell.Value.CellName + "</div></font></td>");
                                }
                                sBuilder.AppendLine("</tr>");
                                sBuilder.AppendLine("<tr>");
                                foreach (var cell in row.Cells)
                                {
                                    if (cell.Value.Equals(mailCell)) { continue; }
                                    sBuilder.AppendLine("<td width='120px' style='border: solid 1 #000000' nowrap=''><font size='2' face='Verdana'><div>" + cell.Value.CellValue + "</div></font></td>");
                                }
                                sBuilder.AppendLine("</tr>");
                                sBuilder.AppendLine(" </tbody>");
                                sBuilder.AppendLine("</table>");
                                try
                                {
                                    MailUtil.SendMail(this.tb_mail_server.Text, mailCell.CellValue, this.tb_mail_showname.Text, this.tb_mail_fromurl.Text, this.tb_mail_sendfrom_pwd.Text, "工资报表", sBuilder.ToString());
                                    this.tb_msg.Text = mailCell.CellValue + " 邮件发送完成 \r\n" + this.tb_msg.Text;
                                }
                                catch (Exception ex)
                                {
                                    showMsg(ex.ToString());
                                    showMsg(mailCell.CellValue + " 邮件发送失败");
                                }
                            }
                            else
                            {
                                showMsg("没有 mail 列 无法发送邮件 ");

                                return;
                            }
                        }
                        isSendMail = false;
                        showMsg("所有工作全部完成 \r\n ");
                    }).Start();
                }
            }
        }
    }
}
